package com.htc.dlc;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.htc.adapter.RetailerAdapter;
import com.htc.common.ActionEvent;
import com.htc.constants.Constants;
import com.htc.controller.RetailersController;
import com.htc.menu.MenuController;
import com.htc.menu.MenuController.MenuSelectedListener;
import com.htc.model.dto.RetailersDTO;
import com.htc.utils.CommonActions;

public class RetailersActivity extends Activity implements
		MenuSelectedListener, OnItemClickListener {
	MenuController menuController;
	View menuView;
	View menuViewSecondary;
	LinearLayout blurimageView;
	CommonActions ca;
	RetailerAdapter adapter;
	ListView lv_home;
	ArrayList<Object> arrData;
	TextView tv_actionbar_title;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_retailers);
		this.ca = new CommonActions(this);
		initViews();
		arrData = new ArrayList<Object>();

		this.menuController = new MenuController(this.menuView,
				this.menuViewSecondary, this.blurimageView, this,
				getBaseContext(), RetailersActivity.this);

		adapter = new RetailerAdapter(this, R.layout.adapter_news, arrData);
		lv_home.setAdapter(adapter);
		initData1();
		lv_home.setOnItemClickListener(this);
	}

	private void initViews() {
		tv_actionbar_title = (TextView) findViewById(R.id.tv_actionbar_title);
		tv_actionbar_title.setText("Retailers");
		lv_home = (ListView) findViewById(R.id.lv_home);
		View localView1 = getLayoutInflater().inflate(R.layout.activity_footer,
				null);
		this.lv_home.addFooterView(localView1);
		this.menuView = findViewById(R.id.menu_container);
		this.menuViewSecondary = findViewById(R.id.menu_secondary);
		this.blurimageView = (LinearLayout) findViewById(R.id.lay_blur_image);

	}

	@Override
	public void onMenuSelected(int paramInt) {
		// TODO Auto-generated method stub

	}

	private void initData1() {

		ActionEvent e = new ActionEvent(this, Constants.GET_RETAILER, null,
				null);
		RetailersController.getInstance().handleViewEvent(e);
	}

	@SuppressWarnings("unchecked")
	public void handleControllerViewEvent(ActionEvent e) {
		switch (e.action) {
		case Constants.GET_RETAILER:
			ArrayList<RetailersDTO> newArr = (ArrayList<RetailersDTO>) e.viewData;
			new VerySlowTask(newArr).execute();
			break;
		default:
			break;
		}
	}

	public class VerySlowTask extends AsyncTask<String, Object, Void> {
		List<RetailersDTO> _newArr;

		public VerySlowTask(List<RetailersDTO> arr) {
			_newArr = arr;
		}

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(String... params) {
			try {
				for (int i = 0; i < _newArr.size(); i++) {
					publishProgress(_newArr.get(i));

				}
			} catch (Exception e) {
				// TODO: handle exception
			}

			return null;
		}

		@Override
		protected void onProgressUpdate(Object... values) {
			arrData.add(values[0]);

		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			adapter.notifyDataSetChanged();
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		//Object o = adapter.getArrObject().get(position);
		try {
			RetailersDTO dto = (RetailersDTO) lv_home.getItemAtPosition(position);
			Intent intent = new Intent(RetailersActivity.this,
					DetailRetailerActivity.class);
			Bundle data = new Bundle();

			SettingsActivity st = new SettingsActivity();
			if (st.flag == 0) {
				data.putString("titler", dto.getRetailerName());
				data.putString("descriptionr", dto.getRetailerDescription());
			} else if (st.flag == 1) {
				data.putString("titler", dto.getRetailerNameAr());
				data.putString("descriptionr", dto.getRetailerDescriptionAr());
			}
			data.putString("imager", dto.getRetailerImage());
			// Bitmap bmp = dto.getRetailerBitmapImage();
			//
			// ByteArrayOutputStream stream = new ByteArrayOutputStream();
			// bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
			// byte[] byteArray = stream.toByteArray();
			//
			// intent.putExtra("imager", byteArray);
			intent.putExtras(data);
			startActivity(intent);
		} catch (Exception e) {
			// TODO: handle exception
		}
		

	}
}
